<?php
class YUtil {
    //裁剪字符串，加“...”
    static function substr($str,$length,$endfix='...') {
        mb_internal_encoding("UTF-8");
        $str_length = mb_strwidth($str);
        if($str_length>$length*2) {
            return mb_substr($str,0,$length).$endfix;
        } else {
            return $str;
        }
    }


    //获得客户端IP
    static function getIp() {
        if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
            $ip = getenv("HTTP_CLIENT_IP");
        else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
                $ip = getenv("HTTP_X_FORWARDED_FOR");
            else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
                    $ip = getenv("REMOTE_ADDR");
                else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
                        $ip = $_SERVER['REMOTE_ADDR'];
                    else
                        $ip = "unknown";
        return($ip);
    }

    //将任意时间字符串转化成时间戳
    static function my_mktime($dtime) {
        if(!ereg("[^0-9]",$dtime)) return $dtime;
        $dt = Array(1970,1,1,0,0,0);
        $dtime = ereg_replace("[\r\n\t]|日|秒"," ",$dtime);
        $dtime = str_replace(".","-",$dtime);
        $dtime = str_replace("年","-",$dtime);
        $dtime = str_replace("月","-",$dtime);
        $dtime = str_replace("时",":",$dtime);
        $dtime = str_replace("分",":",$dtime);
        $dtime = trim(ereg_replace("[ ]{1,}"," ",$dtime));
        $ds = explode(" ",$dtime);
        $ymd = explode("-",$ds[0]);
        if(isset($ymd[0])) $dt[0] = $ymd[0];
        if(isset($ymd[1])) $dt[1] = $ymd[1];
        if(isset($ymd[2])) $dt[2] = $ymd[2];
        if(strlen($dt[0])==2) $dt[0] = '20'.$dt[0];
        if(isset($ds[1])) {
            $hms = explode(":",$ds[1]);
            if(isset($hms[0])) $dt[3] = $hms[0];
            if(isset($hms[1])) $dt[4] = $hms[1];
            if(isset($hms[2])) $dt[5] = $hms[2];
        }
        foreach($dt as $k=>$v) {
            $v = ereg_replace("^0{1,}","",trim($v));
            if($v=="") $dt[$k] = 0;
        }
        $mt = @mktime($dt[3],$dt[4],$dt[5],$dt[1],$dt[2],$dt[0]);
        if($mt>0) return $mt;
        else return time();
    }

    //读取远程文件内容，使用curl库，有超时设置
    static function my_file_get_contents($url, $timeout=10) {
        if ( function_exists("curl_init") ) {
            $ch = curl_init();
            curl_setopt ($ch, CURLOPT_URL, $url);
            curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);
            $file_contents = curl_exec($ch);
            curl_close($ch);
        } else if ( ini_get("allow_url_fopen") == 1 || strtolower(ini_get("allow_url_fopen")) == "on" ) {
                $file_contents = file_get_contents($url);
            } else {
                $file_contents = "";
            }
        return $file_contents;
    }
    //裁剪字符串，不加“...”
    static function cutstr($str, $startstr, $endstr) {
        $length = strlen($str);
        $start = mb_strpos($str, $startstr);
        $str = substr($str,$start,$length-$start);

        $end = mb_strpos($str,$endstr);
        return mb_substr($str, 0, $end);
    }

    //自动探测字符编码，并转换到指定编码
    static function my_encoding($data,$to) {
        $encode_arr = array('UTF-8','GBK','GB2312','BIG5');
        $encoded = mb_detect_encoding($data, $encode_arr);
        $data = mb_convert_encoding($data,$to,$encoded);
        return $data;
    }

    static function num2rmb ($num) {
        $c1="零壹贰叁肆伍陆柒捌玖";
        $c2="分角元拾佰仟万拾佰仟亿";
        $num=round($num,2);
        $num=$num*100;
        if(strlen($num)>10) {
            return "oh,sorry,the number is too long!";
        }

        $i=0;
        $c="";
        while (1) {
            if($i==0) {
                $n=substr($num,strlen($num)-1,1);
            }else {
                $n=$num %10;
            }

            $p1=substr($c1,3*$n,3);
            $p2=substr($c2,3*$i,3);
            if($n!='0' || ($n=='0' &&($p2=='亿' || $p2=='万' || $p2=='元' ))) {
                $c=$p1.$p2.$c;
            }else {
                $c=$p1.$c;
            }

            $i=$i+1;
            $num=$num/10;
            $num=(int)$num;

            if($num==0) {
                break;
            }
        }
        $j = 0;
        $slen=strlen($c);
        while ($j< $slen) {
            $m = substr($c,$j,6);

            if ($m=='零元' || $m=='零万' || $m=='零亿' || $m=='零零') {
                $left=substr($c,0,$j);
                $right=substr($c,$j+3);
                $c = $left.$right;
                $j = $j-3;
                $slen = $slen-3;
            }
            $j=$j+2;
        }

        if(substr($c,strlen($c)-3,3)=='零') {
            $c=substr($c,0,strlen($c)-3);
        }

        if(substr($c,-3,3)=='元') {
            return $c."整";
        }else {
            return $c;
        }
    }

    /**
     * 根据当前时间生成 18 为不重复的数字字符
     * 注：后四位为随机数
     * @return string
     */
    static function genIdByTime() {
        return date('Ymdhms', time()) . mt_rand(1000, 9999);
    }

    /**
     * 解析字符串成数组
     * node1:1,node2:2 转换成如下数组:
     * array(
     *     'node1' => 1,
     *     'node2' => 2,
     * )
     * @param $string
     * @return array
     */
    static function string2Array($string) {
        $tmp = split(',', $string);
        //		QDebug::dump($tmp);
        $ret = array();
        if (count($tmp) >= 1) {
            foreach ($tmp as $arg) {
                $tmp_arg = split(':', $arg);
                if (count($tmp_arg) == 2) {
                    $ret = array_merge($ret, array($tmp_arg[0] => $tmp_arg[1]));
                }
            }
        }

        return $ret;
    }

    /**
     * 求两个时间的时间差
     * @param $d1
     * @param $d2
     * @return integer
     */
    static function dateDiff($d1, $d2) {
        if (is_string($d1)) $d1 = strtotime($d1);
        if (is_string($d2)) $d2 = strtotime($d2);
        return ($d2 - $d1)/43200; //这里返回的是天数，如果除以3600就是返回小时了，依此类推
    }

}